package com.njlime.cms.service.sys;

import com.njlime.cms.entities.MaterialsImage;
import com.njlime.cms.entities.MaterialsNews;
import com.njlime.cms.repository.sys.LiveDao;
import com.njlime.cms.repository.sys.MaterialImageDao;
import com.njlime.cms.repository.sys.MaterialImageGroupDao;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.StringUtil;
import com.njlime.member.entiies.TMember;
import com.njlime.member.repository.sms.MemberDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by Dean on 2017-7-27.
 */
@Service
@Transactional(readOnly = true)
public class MaterialImageService {

    @Resource
   public MaterialImageDao materialImageDao;
    @Resource
    public MaterialImageGroupDao materialImageGroupDao;

    public MaterialsImage get(int id,int unitId){
        return materialImageDao.get(id,unitId);
    }

    public PageBean getImageList(PageBean pageBean, int unitId,int groupId,String name) {
        return materialImageDao.getImageList(pageBean,unitId, groupId,  name);
    }

    @Transactional(readOnly = false)
    public void delImage(String id, int unitId) {
        if(!StringUtil.isEmpty(id)) {
            String[] ids = id.split(",");
            for(String imgId : ids) {
                materialImageDao.delImage(Integer.valueOf(imgId), unitId);
                MaterialsImage image = materialImageDao.findOne(Integer.valueOf(imgId));
                // 减分组数量
                if (image.getImage_group_id() != null && image.getImage_group_id().intValue() > 0) {
                    materialImageGroupDao.downCount(image.getImage_group_id(), unitId);
                }
            }
        }
    }

    @Transactional(readOnly = false)
    public void save(MaterialsImage info) {
        materialImageDao.save(info);
        if(info.getImage_group_id().intValue() > 0) {
            // 新增分组数量
            materialImageGroupDao.upCount(info.getImage_group_id(), info.getUnit_id());
        }
    }
    /**
     * 图片分组
     * @param ids 图片id
     * @param groupId 分组id
     * @param unitId 单位id
     */
    @Transactional(readOnly = false)
    public void editImageGroup(String ids, int groupId, int unitId) {
        if(ids.indexOf(",")>0){
            for(String id :ids.split(",")){
                MaterialsImage image =  materialImageDao.findOne(Integer.parseInt(id));
                // 减分组数量
                if(image.getImage_group_id() != null && image.getImage_group_id().intValue() >0){
                    materialImageGroupDao.downCount(image.getImage_group_id(),unitId);
                }
                if(groupId > 0) {
                    // 新增分组数量
                    materialImageGroupDao.upCount(groupId, unitId);
                }
                materialImageDao.editImageGroup(Integer.parseInt(id), groupId, unitId);
            }
        }else {
            MaterialsImage image =  materialImageDao.findOne(Integer.parseInt(ids));
            // 减分组数量
            if(image.getImage_group_id() != null && image.getImage_group_id().intValue() >0){
                materialImageGroupDao.downCount(image.getImage_group_id(),unitId);
            }
            if(groupId > 0) {
            // 新增分组数量
              materialImageGroupDao.upCount(groupId,unitId);
            }
            materialImageDao.editImageGroup(Integer.parseInt(ids), groupId, unitId);

        }
    }
    /**
     * 修改分组
     * @param id 图片id
     * @param name 分组id
     * @param unitId 单位id
     */
    @Transactional(readOnly = false)
    public void editGroup(String id, String name, int unitId) {
        materialImageGroupDao.editGroup( id,  name,  unitId);

    }

    @Transactional(readOnly = false)
    public void deleteGroup(String id, int unitId) {
        materialImageGroupDao.deleteGroup( id,    unitId);
    }
	public MaterialsImage findOne(Integer materialsId) {
        return  materialImageDao.findOne(materialsId);
    }
}
